<?php
namespace common\queue\jobs;

use think\queue\Job;
use common\queue\JobAbstract;

use common\service\SteamService;
use common\model\sell66\UserSteam;

use think\Exception;
use think\Log;

class UserSteamJob extends JobAbstract
{

	private $apiKey = '8E8E3E3360E8D07396CC992364503957';

	public function userSteamJob(Job $job, $steamId)
	{
		try {
			$info = UserSteam::getInfoBySteamId($steamId);
			if (!$info) {
				throw new Exception('query user steam failed, steamid:'.$steamId);
			}

			$res1 = SteamService::getInstance()->getSteamLevel($this->apiKey, $steamId);
			$res2 = SteamService::getInstance()->getOwnedGames($this->apiKey, $steamId);
			$res3 = SteamService::getInstance()->getFriendList($this->apiKey, $steamId);

			$playerLevel = isset($res1['player_level'])	? $res1['player_level']	: $info['player_level'];
			$gameCount   = isset($res2['game_count'])	? $res2['game_count']	: $info['game_count'];
			$friendCount = isset($res3['friends'])	? count($res3['friends']) : $info['friend_count'];

			$res = UserSteam::updateUserBySteamId($steamId, $playerLevel, $gameCount, $friendCount);
			if (!$res) {
				throw new Exception('update user info failed, steamid:'.$steamId);
			}

			$job->delete();
			echo 'update user info success, steamid:'.$steamId."\r\n";
		} catch (Exception $e) {
			echo $e->getMessage()."\r\n";
			$job->delete();
			return false;
		}
	}
}
